﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

#endregion

using System;

namespace ScrumTable.Common.Excel.OpenXmlSdk.Extensions
{
    /// <summary>
    /// Provides the additional functionalities required for Excel
    /// data management of a DataSet instance.
    /// </summary>
    public class DataValue
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets an empty data value instance.
        /// </summary>
        public static readonly DataValue Empty = new DataValue();

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the underlying cell value.
        /// </summary>
        public object Value { get; private set; }

        /// <summary>
        /// Gets the underlying link around de cell value.
        /// </summary>
        public Uri Link { get; private set; }
        
        /// <summary>
        /// Gets the internal data reference in the Excel sheet.
        /// </summary>
        public CellReference Reference { get; private set; }

        /// <summary>
        /// Returns true if the current data value entry contains no value.
        /// </summary>
        public bool IsEmpty { get { return (Value == null); } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new immutable DataValue instance.
        /// </summary>
        /// <param name="value">Specifies the raw cell value in typed format.</param>
        /// <param name="link">Specifies the link which is placed around the value.</param>
        /// <param name="reference">Specifies the internal data reference in the Excel sheet.</param>
        public DataValue(object value, Uri link, CellReference reference)
        {
            Value = value;
            Link = link;
            Reference = reference;
        }

        /// <summary>
        /// Creates a new immutable DataValue instance.
        /// </summary>
        /// <param name="value">Specifies the cell value.</param>
        /// <param name="link">Specifies the link which is placed around the value.</param>
        private DataValue(object value = null, Uri link = null)
        {
            Value = value;
            Link = link;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="left">Specifies the left object.</param>
        /// <param name="right">Specifies the right object.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(DataValue left, DataValue right)
        {
            // If both are null, or both are same instance, return true.
            if (ReferenceEquals(left, right))
            {
                return true;
            }

            // If one is null, but not both, return false.
            if ((object)left == null || (object)right == null)
            {
                return false;
            }

            // Return true if the fields match:
            return left.Equals(right);
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="left">Specifies the left object.</param>
        /// <param name="right">Specifies the right object.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(DataValue left, DataValue right)
        {
            return !(left == right);
        }

        /// <summary>
        /// Determines whether the specified IDataObject is equal to the current object.
        /// </summary>
        /// <param name="toCompare">Parameter to check.</param>
        /// <returns>Returns true if the objects are equal.</returns>
        public override bool Equals(object toCompare)
        {
            // If parameter cannot be cast to DomainBase return false.
            return Equals(toCompare as DataValue);
        }

        /// <summary>
        /// Determines whether the specified IDataObject is equal to the current object.
        /// </summary>
        /// <param name="obj">Parameter to check.</param>
        /// <returns>Returns true if the objects are equal.</returns>
        public bool Equals(DataValue obj)
        {
            // If parameter is null return false:
            if ((object)obj == null) // force compiler to use Object::operator==()
                return false;

            if (obj.Value != null) { return obj.Equals(Value); }
            if (Value != null) { return Value.Equals(obj); }
            return true;
        }

        /// <summary>
        /// Returns the hash code for this IDataObject.
        /// </summary>
        /// <returns>Returns the computed hash value.</returns>
        public override int GetHashCode()
        {
            return ((Value != null) ? Value.GetHashCode() : 0);
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return (Value != null) ? ObjectUtil.ConvertToString(Value) : string.Empty;
        }

        /// <summary>
        /// Creates a new DataValue instance with the reference and formula 
        /// data of the current instance and sets the new value.
        /// </summary>
        /// <param name="value">Specifies the cell value.</param>
        /// <param name="link">Specifies the link which is placed around the value.</param>
        /// <returns>Returns the newly created instance.</returns>
        public DataValue ChangeValue(object value, Uri link = null)
        {
            return new DataValue(value, link, Reference);
        }

        /// <summary>
        /// Converts the given object into a data value object. This is usefull
        /// for comparing th DataValue with another.
        /// </summary>
        /// <param name="toConvert">Specifies the value to convert into a data value object.</param>
        internal static DataValue ToValue(object toConvert)
        {
            return new DataValue(toConvert);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}